﻿using System;
using System.Windows;
using Kiva7.Images;
using Kiva7.Translations;
using Kiva7.DataRequests;
using Kiva7.Utilities;

namespace Kiva7.Pages
{
    public partial class JournalEntryPage
    {
        private const string JournalEntryIdKey = "journalEntryId";
        private const string LoanIdKey = "loanId";

        private const string Url = "/Pages/JournalEntryPage.xaml";
        public readonly static Uri PageUrl = new Uri(Url, UriKind.RelativeOrAbsolute);

        public static Uri GetPageUrl(int journalEntryId, int loanId)
        {
            UrlCreator url = new UrlCreator(Url);
            url.Add(JournalEntryIdKey, journalEntryId);
            url.Add(LoanIdKey, loanId);

            return url.ToUri(UriKind.RelativeOrAbsolute);
        }

        public JournalEntryPage()
        {
            InitializeComponent();
            CreateStandardControls();
        }

        LenderJournalEntriesRequest _journalEntriesRequest;
        CommentsRequest _commentsRequest;

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            int journalEntryId;
            int loanId;
            if (int.TryParse(NavigationContext.QueryString[JournalEntryIdKey], out journalEntryId))
            {
                if (_journalEntriesRequest == null
                    && int.TryParse(NavigationContext.QueryString[LoanIdKey], out loanId))
                {
                    _journalEntriesRequest = new LenderJournalEntriesRequest();
                    DataContext = _journalEntriesRequest;
                    _journalEntriesRequest.GetJournalEntries(loanId, false, journalEntryId);
                }

                if (_commentsRequest == null)
                {
                    _commentsRequest = new CommentsRequest();
                    commentsGrid.DataContext = _commentsRequest;
                    _commentsRequest.GetComment(journalEntryId);
                }
            }
        }

        protected override void CreateApplicationBar()
        {
            AddBarButton(AppResources.TranslateTexts, ImageUrls.Translation, TranslateClick);
            AddBarButton(AppResources.Speak, ImageUrls.SpeachBubble, Speak);
        }

        private void TranslateClick(object sender, EventArgs e)
        {
            journalTranslation.TranslateTextWithDetect();
        }

        public void Speak(object sender, EventArgs e)
        {
            media.Speak();
        }

    }
}
